function Convert2D3D(nfrontstart, nsidestart)

display('Running conversion....');

savefile = 'videoprocessdata.mat';
load(savefile);


load(calib2d3dfilefrontvertfull);
vertdp = dp;
vertds = ds;

bswitchfrontside = 0;
maxframesextrapolation = 10;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%pointx
v1pointx = bbx;

load(calib2d3dfilesidehorzfull);
load(calib2d3dfilefronthorzfull);


if bswitchfrontside == 1
    ntemp1 = nsidestart;
    ntemp2 = nfrontstart;

    nfrontstart = ntemp1;
    nsidestart = ntemp2;

    stemp1 = calib2d3dfilefronthorzfull;
    stemp2 = calib2d3dfilesidehorzfull;

    calib2d3dfilefronthorzfull = stemp2;
    calib2d3dfilesidehorzfull = stemp1;

    stemp1 = frontviewfilefull;
    stemp2 = sideviewfilefull;

    frontviewfilefull = stemp2;
    sideviewfilefull = stemp1;
end

ndiff1 = 0;
ndiff2 = 0;

if nfrontstart > nsidestart
    ndiff1 = nfrontstart - nsidestart;
elseif nsidestart > nfrontstart
    ndiff2 = nsidestart - nfrontstart;
end

ffront = frontviewfilefull;
fside = sideviewfilefull;
fout = finaloutputfilefull;



if isempty(ffront) | isempty(fside) | isempty(fout)
    return
end

bPostProcess = 1;

%%%%%%%%%%%%%%%%Load front view file
load(ffront);
v1bAssignGender = exist('gendertypes');


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%pointx

len = length(objs);

if v1bAssignGender == 1
    if gendertypes(1,1) == -1
        v1bAssignGender = 0;
    end       
    
    v1genders = gendertypes;
    v1bgenderwarnings = bgenderwarnings;
else
    v1genders = -ones(len, nfindobjs);
    v1bgenderwarnings = -ones(len, 1);
end


v1objs = objs; 
v1x1s = x1s;
v1y1s = y1s;
v1x2s = x2s;
v1y2s = y2s;




%v1merged = merged;
v1nprocessstart = nprocessstart;


%%%%%%%%%%%%%%%%Load side view file
load(fside);

v2bAssignGender = exist('gendertypes');

len = length(objs);

if v2bAssignGender == 1
    if gendertypes(1,1) == -1
        v2bAssignGender = 0;
    end        
    
    v2genders = gendertypes;
    v2bgenderwarnings = bgenderwarnings;
else
    v2genders = -ones(len, nfindobjs);
    v2bgenderwarnings = -ones(len, 1);
end

v2objs = objs; 
v2x1s = x1s;
v2y1s = y1s;
v2x2s = x2s;
v2y2s = y2s;
v2nprocessstart = nprocessstart;

nprocessdiff1 = 0;
nprocessdiff2 = 0;


if (ndiff1+ndiff2) > 0
    if v2nprocessstart > v1nprocessstart
        nprocessdiff1 = v2nprocessstart - v1nprocessstart;
    elseif v1nprocessstart > v2nprocessstart
        nprocessdiff2 = v1nprocessstart - v2nprocessstart;
    end
end


nfrontdataindexstart = nprocessdiff1 + ndiff1;
nsidedataindexstart = nprocessdiff2 + ndiff2;



len1 = length(v1objs);
len2 = length(v2objs);

len = min(len1-nfrontdataindexstart, len2-nsidedataindexstart);




v1x0s = zeros(len1, nfindobjs);
v1y0s = zeros(len1, nfindobjs);
v2x0s = zeros(len2, nfindobjs);
v2y0s = zeros(len2, nfindobjs);

ax1s = zeros(len, nfindobjs);
ax2s = zeros(len, nfindobjs);
ay1s = zeros(len, nfindobjs);
ay2s = zeros(len, nfindobjs);
az1s = zeros(len, nfindobjs);
az2s = zeros(len, nfindobjs);

ax0s = zeros(len, nfindobjs);
ay0s = zeros(len, nfindobjs);
az0s = zeros(len, nfindobjs);

gendertypes = -ones(len, nfindobjs);
bgenderwarnings = -ones(len, 1);



if v1bAssignGender == 1 & v2bAssignGender == 1
    bAssignGender = 1;
else
    bAssignGender = 0;
end
               



for i = 1:len1
    v1x0s(i,:) = ((v1x2s(i,:)-v1x1s(i,:))/2.0)+v1x1s(i,:);
    v1y0s(i,:) = ((v1y2s(i,:)-v1y1s(i,:))/2.0)+v1y1s(i,:);                
end

for i = 1:len2
    v2x0s(i,:) = ((v2x2s(i,:)-v2x1s(i,:))/2.0)+v2x1s(i,:);
    v2y0s(i,:) = ((v2y2s(i,:)-v2y1s(i,:))/2.0)+v2y1s(i,:);
end



%data indexes
i1s = zeros(len, 1);
i2s = zeros(len, 1);

%video frame indexes
vi1s = zeros(len, 1);
vi2s = zeros(len, 1);


for i = 1:len1
    ncount = 0;
    
    for j =1:nfindobjs
        if v1x0s(i,j) > 0
            ncount = ncount + 1;
        end
    end
    
    v1objs(i) = ncount;    
end


for i = 1:len2
    ncount = 0;
    
    for j =1:nfindobjs
        if v2x0s(i,j) > 0
            ncount = ncount + 1;
        end
    end
    
    v2objs(i) = ncount;
end




bassignments = zeros(len,1);
bassigntypes = zeros(len,1);
									
v1assigns = zeros(len, nfindobjs);
v2assigns = zeros(len, nfindobjs);
            
v1bassigned = zeros(len, nfindobjs);
v2bassigned = zeros(len, nfindobjs);
                        


nminassignsments = 3; 
buf = 0;
v1pointx = imgx - v1pointx + buf;


disp('Assigning objects...');




i = 1;

i1 = nfrontdataindexstart + i;
i2 = nsidedataindexstart + i;

if bAssignGender == 1 & nfindobjs == 2

 while i1 < len & i2 < len 
                     
	i1 = nfrontdataindexstart + i;
	i2 = nsidedataindexstart + i;
    
    vi1 = i1 + v1nprocessstart-1;
    vi2 = i2 + v2nprocessstart-1;
		
	nobjs1= v1objs(i1);
    nobjs2 = v2objs(i2);
    
       
     	
	if nobjs1 == nfindobjs & nobjs2 == nfindobjs & v1bgenderwarnings(i1) == 0 &... 
         v2bgenderwarnings(i2) == 0 & v1genders(i1,1) > -1 & v2genders(i2,1) > -1
        
	  for j = 1:nfindobjs  
		 for k = 1:nfindobjs                                                                                              
			if v1genders(i1,j) == v2genders(i2,k) %0 = male, 1 = female
				v1assigns(i1,j) = k;			
                v2assigns(i2,k) = j;
                                                            
                v1bassigned(i1,j) = 1;
                v2bassigned(i2,k) = 1;       
                
                bassignments(i1) = 1;        
			end
         end
      end      
       
      
    else %if nobjs1 == nfindobjs & nobjs2 == nfindobjs
			 if v1x0s(i1,1) == v1x0s(i1,2) & v2x0s(i2,1) ~= v2x0s(i2,2)                 
                 v2assigns(i2,1) = 1;
                 v2assigns(i2,2) = 1;                 
                 
                 v2bassigned(i2,:) = 1;
                 
                 bassignments(i1) = 1;                        
             elseif v2x0s(i2,1) == v2x0s(i2,2) & v1x0s(i1,1) ~= v1x0s(i1,2)                 
                 v1assigns(i1,1) = 1;
                 v1assigns(i1,2) = 1;
                 
                 v1bassigned(i1,:) = 1;
                                  
                 bassignments(i1) = 1;                              
             elseif v1x0s(i1,1) == v1x0s(i1,2) & v2x0s(i2,1) == v2x0s(i2,2)
                 v1assigns(i1,1) = 1;
                 v1assigns(i1,2) = 2;   
                 
                 v1bassigned(i1,:) = 1;     
                 
                 bassignments(i1) = 1;             
             end
     end
     
     i = i + 1;
  end
  
else

   %Assign objects in front view to objects in side view if objects not assigned 
   %by gender (in which case bAssigngender must be true and nfindobjs must 2).
   while i1 < len & i2 < len %& (bAssignGender == 0 || nfindobjs ~= 2)
    
     nstart = i;
         
    
     nobjsv1 = v1objs(i1);
     nobjsv2 = v2objs(i2);
          
	 while i1 <= len & i2 <= len & v1objs(i1) == nobjsv1 & v2objs(i2) == nobjsv2 
		i1 = nfrontdataindexstart + i;
		i2 = nsidedataindexstart + i;
		
		i = i + 1;
        
     end
    
     nend = i;               
     
     bassignmentsmade = 0;
     dcounts = zeros(nfindobjs, nfindobjs);

     
     if bAssignGender == 1 & nfindobjs == 2
         if sum(v1bgenderwarnings(nfrontdataindexstart+nstart:nfrontdataindexstart+nend)) == 0 
			bgenwarn1 = 0;
         end
         
         if sum(v2bgenderwarnings(nsidedataindexstart+nstart:nsidedataindexstart+nend)) == 0 
			bgenwarn2 = 0;
         end
     end                     
     
     nassignments = 0;


     if nend > len 
         nend = len;
     end
           
            
     b1count = 0;
     b2count = 0;
       
     for l = nstart:nend
     
	   i1 = nfrontdataindexstart + l;
	   i2 = nsidedataindexstart + l;
       
       vi1 = i1 + v1nprocessstart-1;
       vi2 = i2 + v2nprocessstart-1; 
		 
	   nobjs1 = v1objs(i1);
	   nobjs2 = v2objs(i2);
       

         
       if nobjs1 > 0 & nobjs2 > 0
              
         nv1xs = imgx-v1x0s(i1,:);
         nv1ys = v1y0s(i1,:);
         
         nv2xs = imgx-v2x0s(i2,:);     
         nv2ys = v2y0s(i2,:);
         
         b = 1;         
         
         
         if bAssignGender == 1 & nobjs1 == nfindobjs & nobjs2 == nfindobjs & nfindobjs == 2 %& bgenwarn1 == 0 & bgenwarn2 == 0
         						
		    for j = 1:nfindobjs  
				for k = 1:nfindobjs                                                                                              
					if v1genders(i1,j) == v2genders(i2,k) %0 = male, 1 = female
						v1assigns(i1,j) = k;			
                        v2assigns(i2,k) = j;
                        
                                    
                        v1bassigned(i1,j) = 1;
                        v2bassigned(i2,k) = 1;
                        
                        bassignments(i1) = 1;
                        
                        dcounts(j, k) = dcounts(j, k) + 1;
                        
                        b = 0;                                                 
					end
				end
            end            
          
         end
         
         if b > 0 & nobjs1 == nfindobjs & nobjs2 == nfindobjs & nfindobjs == 2
             if v1x0s(i1,1) == v1x0s(i1,2) & v2x0s(i2,1) ~= v2x0s(i2,2)
                 
                 v2assigns(i2,1) = 1;
                 v2assigns(i2,2) = 1;
                 
                 v2bassigned(i2,:) = 1;
                 
                 bassignments(i1) = 1;
                        
                 b = 0;
             elseif v2x0s(i2,1) == v2x0s(i2,2) & v1x0s(i1,1) ~= v1x0s(i1,2)
                 
                 v1assigns(i1,1) = 1;
                 v1assigns(i1,2) = 1;
                 
                 v1bassigned(i1,:) = 1;
                                  
                 bassignments(i1) = 1;
                 
                 b = 0;
             elseif v1x0s(i1,1) == v1x0s(i1,2) & v2x0s(i2,1) == v2x0s(i2,2)
                 v1assigns(i1,1) = 1;
                 v1assigns(i2,1) = 2;   
                 
                 v1bassigned(i1,:) = 1;              
                        
                 bassignments(i1) = 1;
                 
                 b = 0;            
             end
         end
         
         if b > 0 & bAssignGender == 0             

             xmaxv1a = 1;
             xminv1a = 10000;
             xmaxv1b = 1;
             xminv1b = 10000;
             
             xmaxv2a = 1;
             xminv2a = 10000;
             xmaxv2b = 1;
             xminv2b = 10000;
             
             ymaxv2a = 1;
             yminv2a = 10000;
 
             
             for j = 1:nfindobjs
                if v1x0s(i1,j) > 0 & nv1xs(j) < v1pointx 
                    if nv1xs(j) > xmaxv1a 
                        xmaxv1a = nv1xs(j);
                    end
                    
                    if nv1xs(j) < xminv1a 
                        xminv1a = nv1xs(j);
                    end
                elseif v1x0s(i1,j) > 0
                    if nv1xs(j) > xmaxv1b 
                        xmaxv1b = nv1xs(j);
                    end
                    
                    if nv1xs(j) < xminv1b
                        xminv1b = nv1xs(j);
                    end
                end
                
                
                 if v2x0s(i1,j) > 0 & nv2xs(j) < v1pointx 
                    if nv2xs(j) > xmaxv2a 
                        xmaxv2a = nv2xs(j);
                    end
                    
                    if nv2xs(j) < xminv2a 
                        xminv2a = nv2xs(j);
                    end
                                        
                    if nv2ys(j) > ymaxv2a 
                        ymaxv2a = nv2ys(j);
                    end
                    
                    if nv2ys(j) < yminv2a 
                        yminv2a = nv2ys(j);
                    end
                    
                elseif v2x0s(i1,j) > 0
                    if nv2xs(j) > xmaxv2b 
                        xmaxv2b = nv2xs(j);
                    end
                    
                    if nv2xs(j) < xminv2b
                        xminv2b = nv2xs(j);
                    end
                 end                    
             end
                   
                    
        
             
            bwarn = 0;
            assigns = zeros(nfindobjs, 1);
            assigntos = zeros(nfindobjs, 1);
            
            maxconst = 10;
            distsa = ones(nfindobjs, nfindobjs)*maxconst;
            
            distsb = ones(nfindobjs, nfindobjs)*maxconst;
            
            btypes = -ones(nfindobjs, nfindobjs);
            
            btype = -1;
  
            
            for j = 1:nfindobjs
                if v1x0s(i1,j) > 0            
					for k = 1:nfindobjs                  
						if v2x0s(i2, k) > 0 
                            
                           if nv1xs(j) < v1pointx & nv2xs(k) < v1pointx                                
                                dist2 = abs( (nv1xs(j)/xmaxv1a) - (nv2ys(k)/ymaxv2a)); 
                               
								distsa(j,k) = dist2; 
                                
                                btype = 0;
                                
                                btypes(j,k) = 0;
                                
                                b1count = b1count + 1;

                           elseif nv1xs(j) >= v1pointx & nv2xs(k) >= v1pointx
                               distsb(j,k) = abs( (nv1xs(j)/xmaxv1b)  - (nv2xs(k)/xmaxv2b) ); 
                               
                               btype = 1;
                               
                               btypes(j,k) = 1;
                               
                               
                               b2count = b2count + 1;
                           end                            
						end
                    end
                    
                    if btype == -1
                        bwarn = 1;
                    end
                          
			   end
            end
            
            minvertdistbetweenfish = 10;
            
              for j = 1:nfindobjs
                  for k = 1:nfindobjs
                      if j ~= k & v1x0s(i1,j) > 0 & v1x0s(i1,k) > 0  
                          if abs(v1x0s(i1,j) - v1x0s(i1,k)) < minvertdistbetweenfish & btype == 1
                              bwarn = 1;
                          end
                      end
                  end
              end
              
              
              for j = 1:nfindobjs
                  for k = 1:nfindobjs
                      if j ~= k & v2x0s(i1,j) > 0 & v2x0s(i1,k) > 0  
                          if abs(v2x0s(i1,j) - v2x0s(i1,k)) < minvertdistbetweenfish & btype == 1
                              bwarn = 1;
                          end
                      end
                  end
              end

            
             if bwarn == 0 & nobjs1 == nobjs2 %& i1 == 166
                           
                 %i1
                 
                 [adists, assignments] = AssignDists(distsa);                 
                 
                 for j = 1:nfindobjs
                     
                     if adists(j) < maxconst
                         if assigntos(assignments(j)) > 0 & nobjs1 == nobjs2
                             bwarn = 1;
                         end
                         assigns(j) = assignments(j);
                         assigntos(assignments(j)) = j;
                     end
                 end        
                                     
                
                 [adists, assignments] = AssignDists(distsb);
                                  
                 
                 for j = 1:nfindobjs
                     
                     if adists(j) < maxconst
                         if assigntos(assignments(j)) > 0 & nobjs1 == nobjs2
                             bwarn = 1;
                         end
                         assigns(j) = assignments(j);
                         assigntos(assignments(j)) = j;
                     end
                 end    

                 
                if bwarn  == 0 & isempty(find(assigns == 0,1))
                           
                    bassignmentsmade = 1;
                    
                                
                    for j = 1:nfindobjs 
                        if nv1xs(j) > 0                                                     
                            dcounts(j, assigns(j)) = dcounts(j, assigns(j)) + 1;                            
                        end
                    end                                
                end
            end
            
            bassigntypes(i1) = b;
            
            nassignments = nassignments + 1;
            
            
          end
        end        
     end
     
     
     if bassignmentsmade == 1 & nassignments > nminassignsments
	
        a = 1:nfindobjs;
        p = perms(a);
        sz = size(p);
        
        pmax = 0;
        
        for j = 1:sz(1)
            pc = 0;
            
            for k = 1:sz(2)
                pc = pc + dcounts(k, p(j, k));
            end
            
            if pc > pmax
                pmax = pc;
            end
        end
                
		i1a = nfrontdataindexstart + nstart;
		i1b = nfrontdataindexstart + nend;
        
        i2a = nsidedataindexstart + nstart;
		i2b = nsidedataindexstart + nend;      

		bassignments(i1a:i1b) = 1;

		for k = 1:nfindobjs 
         
			dmax = 0;
    
			for j = 1:nfindobjs 
				[d, index] = max(dcounts(j,:));
            
				if d > dmax
					dmax = d;
					jassign = j;
					assignedto = index;        
				end
			end        
               
			
			dcounts(:,assignedto) = 0;
			dcounts(jassign,:) = 0;       
									
			v1assigns(i1a:i1b,jassign) = assignedto;
            v2assigns(i2a:i2b,assignedto) = jassign;
            
            v1bassigned(i1a:i1b,jassign) = 1;
            v2bassigned(i2a:i2b,assignedto) = 1;
        end        
     end       
  end
end



a = find(bassignments > 0);

disp('% of objects assigned:');
length(a)/(len-nfrontdataindexstart)


disp('Objects assigned, calculating three-dimensional coordinates...');



bCoordinatesCalculated = zeros(len,1);

         
v1ymin = 100000;
v1ymax = 0;

v2ymin = 100000;
v2ymax = 0;

for j = 1:nfindobjs         
    a = find(v1y1s(:,j) > 0);
    b = find(v2y1s(:,j) > 0);
    
	v1ymin = min( [v1ymin; v1y1s(a,j)]);
	v1ymax = max( [v1ymax; v1y2s(a,j)]);
	
	v2ymin = min( [v2ymin; v2y1s(b,j)]);
	v2ymax = max( [v2ymax; v2y2s(b,j)]);
end
          
v1ymin = fblx; %(fblx+fflx)/2.0;
v1ymax = fbrx; %(fbrx+ffrx)/2.0;
v2ymin = sblx; %(sblx+sflx)/2.0;
v2ymax = sbrx; %(sbrx+sfrx)/2.0;

v1ydiff = abs(v1ymin-v1ymax);
v2ydiff = abs(v2ymin-v2ymax);

i = 1;
i1 = 1;
i2 = 1;


while i1 < len & i2 < len 
     
     if mod(i, 1000) == 0
        [i, len]
     end         
        
     i1 = nfrontdataindexstart + i;
     i2 = nsidedataindexstart + i;
        
     vi1 = i1 + v1nprocessstart-1;
     vi2 = i2 + v2nprocessstart-1;
        
        
     i1s(i) = i1;
     i2s(i) = i2;
        
     vi1s(i) = vi1;
     vi2s(i) = vi2;
        
    
     v1obj = v1objs(i1);
	 v2obj = v2objs(i2);        
              
     v1bsameobjs = zeros(nfindobjs, 1);
     v2bsameobjs = zeros(nfindobjs, 1);
     
     v1sameobjsmaxy = zeros(nfindobjs, 1);
     v2sameobjsminy = ones(nfindobjs, 1)*1000000;
              
     for j = 1:nfindobjs   

        v2index = v1assigns(i1,j);
        v1index = v2assigns(i2,j);        
        
        b1same = 0;
        b2same = 0;

        for k = 1:nfindobjs 
        	if j ~= k
        		if v2index > 0 & v1assigns(i1,k) == v2index
	        		b1same = 1;
	        		v1sameobjsmaxy(v2index) = max([v1sameobjsmaxy(v2index), v1y0s(i1,j), v1y0s(i1,k)]); 
        		end
        		
        		if v1index > 0 & v2assigns(i2,k) == v1index
	        		b2same = 1;
					v2sameobjsminy(v1index) = min([v2sameobjsminy(v1index), v2y0s(i2,j), v2y0s(i2,k)]);
        		end        		
        	end
		end
		
		v1bsameobjs(j) = b1same;
	    v2bsameobjs(j) = b2same;
     end    
    
     if v1obj > 0 && sum(v1y1s(i1,:)) > 0                
               
        if bassignments(i1) == 1                            
            
            
            nv1assignments = sum(v1bassigned(i1,:));
            nv2assignments = sum(v2bassigned(i2,:));            
            
            [nobjs, index] = max( [nv1assignments, nv2assignments]);
            
            for j = 1:nfindobjs
                
                v1index = j;                
                v2index = v1assigns(i1,j);            
                
                if index == 1 & v1index > 0 & v2index > 0
                    
                    
                    v1x0 = v1x0s(i1, v1index);
                    v1y0 = v1y0s(i1, v1index);
                    v1y1 = v1y1s(i1, v1index);
                    v1y2 = v1y2s(i1, v1index);
                    v1x1 = v1x1s(i1, v1index);
                    v1x2 = v1x2s(i1, v1index);
                    
                    v2x0 = v2x0s(i2, v2index);
                    v2y0 = v2y0s(i2, v2index);
                    v2y1 = v2y1s(i2, v2index);
                    v2y2 = v2y2s(i2, v2index);
                    v2x1 = v2x1s(i2, v2index);
                    v2x2 = v2x2s(i2, v2index);
                    
                    
                    if v1bsameobjs(j) == 1 & v1y0s(i1, j) < v1sameobjsmaxy(v2index)	                    
                        objydiff = abs(v1sameobjsmaxy(v2index)-v1y0s(i1, j));
                        sdiff = abs(sbpmm-sfpmm);
                        dscale = (sbpmm+(sdiff/v1ydiff)*(v1ydiff-objydiff))/(sfpmm+sfpmm*0.0);
                        
                        newwidth = abs(v2y1s(i2, v2index)-v2y2s(i2, v2index))*dscale;
	                    newheight = abs(v2x1s(i2, v2index)-v2x2s(i2, v2index))*dscale;
                        
	                    v2y1 = v2y0-(newwidth/2.0);
	                    v2y2 = v2y0+(newwidth/2.0);
	                    v2x1 = v2x0-(newheight/2.0);
	                    v2x2 = v2x0+(newheight/2.0);
                    end
                elseif v1index > 0 & v2index > 0
                    v2index = j;                
                    v1index = v2assigns(i2,j);
                    
                    v1x0 = v1x0s(i1, v1index);
                    v1y0 = v1y0s(i1, v1index);
                    v1y1 = v1y1s(i1, v1index);
                    v1y2 = v1y2s(i1, v1index);
                    v1x1 = v1x1s(i1, v1index);
                    v1x2 = v1x2s(i1, v1index);
                    
                    v2x0 = v2x0s(i2, v2index);
                    v2y0 = v2y0s(i2, v2index);
                    v2y1 = v2y1s(i2, v2index);
                    v2y2 = v2y2s(i2, v2index);
                    v2x1 = v2x1s(i2, v2index);
                    v2x2 = v2x2s(i2, v2index);
                    
                    if v2bsameobjs(j) == 1 & v2y0s(i2, j) > v2sameobjsminy(v1index)	                    
                        objydiff = abs(v2sameobjsminy(v1index)-v2y0s(i2, j));
                        fdiff = abs(fbpmm-ffpmm);
                        dscale = (fbpmm+(fdiff/v2ydiff)*(v2ydiff-objydiff))/(ffpmm+ffpmm*0.0);
                        
                        
	                    newwidth = abs(v1y1s(i2, v1index)-v1y2s(i1, v1index))*dscale;
	                    newheight = abs(v1x1s(i2, v1index)-v1x2s(i1, v1index))*dscale;
	                    	   
	                    v1y1 = v1y0-(newwidth/2.0);
	                    v1y2 = v1y0+(newwidth/2.0);
	                    v1x1 = v1x0-(newheight/2.0);
	                    v1x2 = v1x0+(newheight/2.0);
                    end
                end                             
                
                if v1index > 0 & v1x0 > 0 & v2index > 0 & v2x0 > 0                                        
                                      
					[aa, ax1, cc] = ConvertPixelToMM(frontbackdist, vertdp, vertds, bpmm, bby, ffrontbackdist, sfrontbackdist, kf, ks, bf, bs, df, ds, as, af, v1x0, v1y1, v2x0, v2y0);                
					[aa, ax2, cc] = ConvertPixelToMM(frontbackdist, vertdp, vertds, bpmm, bby, ffrontbackdist, sfrontbackdist, kf, ks, bf, bs, df, ds, as, af, v1x0, v1y2, v2x0, v2y0);                                                
                                
					[aa, bb, ay1] = ConvertPixelToMM(frontbackdist, vertdp, vertds, bpmm, bby, ffrontbackdist, sfrontbackdist, kf, ks, bf, bs, df, ds, as, af, v1x0, v1y0, v2x0, v2y1);
					[aa, bb, ay2] = ConvertPixelToMM(frontbackdist, vertdp, vertds, bpmm, bby, ffrontbackdist, sfrontbackdist, kf, ks, bf, bs, df, ds, as, af, v1x0, v1y0, v2x0, v2y2);
                
					[az1, bb, cc] = ConvertPixelToMM(frontbackdist, vertdp, vertds, bpmm, bby, ffrontbackdist, sfrontbackdist, kf, ks, bf, bs, df, ds, as, af, v1x2, v1y0, v2x2, v2y0);
					[az2, bb, cc] = ConvertPixelToMM(frontbackdist, vertdp, vertds, bpmm, bby, ffrontbackdist, sfrontbackdist, kf, ks, bf, bs, df, ds, as, af, v1x1, v1y0, v2x1, v2y0);														
                    	
                    
                    if ax1 > 0 & ax2 > 0 & ay1 > 0 & ay2 > 0 & az1 > 0 & az2 > 0
                                               
                    
                        v1gen = v1genders(i1, v1index);
                        v2gen = v2genders(i2, v2index);
                        
                        bgen1 = v1bgenderwarnings(i1);
                        bgen2 = v2bgenderwarnings(i2);
                        
                        if v1gen > -1 & v2gen > -1
                            if v1gen == v2gen
                                gendertypes(i,j) = v1gen; 
                            elseif bgen1 == 0 & bgen2 == 1
                                gendertypes(i,j) = v1gen;                                
                            elseif bgen1 == 1 & bgen2 == 0
                                gendertypes(i,j) = v2gen;                     
                            end
                        elseif v1gen > -1
                            gendertypes(i,j) = v1gen;
                            bgenderwarnings(i) = bgenderwarnings(i) + bgen1;
                        elseif v2gen > -1                                    
                            gendertypes(i,j) = v2gen;    
                            bgenderwarnings(i) = bgenderwarnings(i) + bgen2;
                        end                                                   
    
                        ax1s(i, j) = ax1;
                        ax2s(i, j) = ax2;
                        ay1s(i, j) = ay1;
                        ay2s(i, j) = ay2;
                        az1s(i, j) = az1;
                        az2s(i, j) = az2;

                        ax0s(i, j) = ((ax2-ax1)/2.0) + ax1;                
                        ay0s(i, j) = ((ay2-ay1)/2.0) + ay1;             
                        az0s(i, j) = ((az2-az1)/2.0) + az1;           
                    
                        if ax0s(i, j) > 0
                            bCoordinatesCalculated(i) = 1;
                        end
                                                
                    end                                    
			    end
                                                         
            end
        end
     
     end
      
     i = i + 1;
end



for j = 1:nfindobjs    [ax0s(:,j), ay0s(:,j), az0s(:,j)] = Extrapolate3dPoints(ax0s(:,j), ay0s(:,j), az0s(:,j), maxframesextrapolation);
    [ax1s(:,j), ay1s(:,j), az1s(:,j)] = Extrapolate3dPoints(ax1s(:,j), ay1s(:,j), az1s(:,j), maxframesextrapolation);
    [ax2s(:,j), ay2s(:,j), az2s(:,j)] = Extrapolate3dPoints(ax2s(:,j), ay2s(:,j), az2s(:,j), maxframesextrapolation);
end


fout
save(fout, 'ax0s', 'ax1s', 'ax2s', 'ay0s', 'ay1s', 'ay2s', 'az0s', 'az1s', 'az2s', 'objs', 'i1s', 'i2s', 'vi1s', 'vi2s', 'gendertypes', 'bgenderwarnings');
